package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CustomerReport;
import cn.wolfcode.crm.query.CustomerReportQueryObject;
import cn.wolfcode.crm.service.ICustomerReportService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo){
        PageInfo result = customerReportService.query(qo);
        model.addAttribute("result",result);
        return "/customerReport/list";
    }

    //柱状图
    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo){
        //获取所有数据结果集(不用分页)
        List<CustomerReport> list = customerReportService.listAll(qo);
        //从结果集中取出x轴和y轴的数据
        ArrayList xlist = new ArrayList();
        ArrayList ylist = new ArrayList();
        for (CustomerReport customerReport : list) {
            //x轴放分组类型,y轴放数量
            xlist.add(customerReport.getGroupType());
            ylist.add(customerReport.getNumber());
        }
        //共享页面的数据,将结果转为json字符串
        model.addAttribute("xlist", JSON.toJSONString(xlist));
        model.addAttribute("ylist",JSON.toJSONString(ylist));
        return "/customerReport/listBar";
    }

    //饼状图
    @RequestMapping("/listPie")
    public String listPie(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo){
        //获取所有数据结果集(不用分页)
        List<CustomerReport> list = customerReportService.listAll(qo);
        //从结果集中取出x轴和y轴的数据
        ArrayList xlist = new ArrayList();
        ArrayList ylist = new ArrayList();
        for (CustomerReport customerReport : list) {
            //观察饼图需要的参数,xlist依然可以用,但ylist要修改一下
            xlist.add(customerReport.getGroupType());
            HashMap<String,Object> map = new HashMap<>();
            map.put("name",customerReport.getGroupType());
            map.put("value",customerReport.getNumber());
            ylist.add(map);
        }
        //共享页面的数据,将结果转为json字符串
        model.addAttribute("xlist", JSON.toJSONString(xlist));
        model.addAttribute("ylist",JSON.toJSONString(ylist));
        return "/customerReport/listPie";
    }
}
